package org.dromara.kitchen.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.kitchen.domain.BdProvince;
import org.dromara.kitchen.service.IBdProvinceService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 全国的 省份
 *
 * @author Mr.Zhou
 * @date 2024-12-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/kitchen/province")
public class BdProvinceController extends BaseController {

    private final IBdProvinceService bdProvinceService;

    /**
     * 查询全国的 省份列表
     */
    @SaCheckPermission("kitchen:province:list")
    @GetMapping("/list")
    public TableDataInfo<BdProvince> list(BdProvince bdProvince, PageQuery pageQuery) {
        QueryWrapper<BdProvince> qw = new QueryWrapper<>();

        TableDataInfo<BdProvince> td = PageUtils.buildDataInfo(bdProvinceService.page(PageUtils.buildPage(),qw));
        List<BdProvince> list = td.getRows();
        return td;
    }

    /**
     * 导出全国的 省份列表
     */
    @SaCheckPermission("kitchen:province:export")
    @Log(title = "全国的 省份", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BdProvince bdProvince, HttpServletResponse response) {
        List<BdProvince> list = bdProvinceService.list();
        ExcelUtil.exportExcel(list, "全国的 省份", BdProvince.class, response);
    }

    /**
     * 获取全国的 省份详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("kitchen:province:query")
    @GetMapping("/{id}")
    public R<BdProvince> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer id) {
        return R.ok(bdProvinceService.getById(id));
    }

    /**
     * 新增全国的 省份
     */
    @SaCheckPermission("kitchen:province:add")
    @Log(title = "全国的 省份", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BdProvince bdProvince) {
        return toAjax(bdProvinceService.save(bdProvince));
    }

    /**
     * 修改全国的 省份
     */
    @SaCheckPermission("kitchen:province:edit")
    @Log(title = "全国的 省份", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BdProvince bdProvince) {
        return toAjax(bdProvinceService.updateById(bdProvince));
    }

    /**
     * 删除全国的 省份
     *
     * @param ids 主键串
     */
    @SaCheckPermission("kitchen:province:remove")
    @Log(title = "全国的 省份", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Integer[] ids) {
        return toAjax(bdProvinceService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
